"""
Case Type   : JDBC查询内存表
Case Name   : JDBC使用desc连接数据库，查询MOT表数据，验证功能
Create At   : 2024.4.2
Owner       : haomeng
Description :
    1.关闭参数enable_incremental_checkpoint
    2.编写java脚本，脚本中创建多个connection,每一个connection下分别创建多个statement
    3.编译java文件
    4.运行java文件
    5.清理环境
Expect      :
    1.关闭参数成功
    2.编写Java脚本成功，从远程备机复制到主机成功
    3.编译成功
    4.运行java程序成功，执行结果预期正常
    5.清理环境成功
History     :
"""

import os
import unittest
from yat.test import Node
from yat.test import macro
from testcase.utils.Logger import Logger
from testcase.utils.Common import Common
from testcase.utils.CommonSH import CommonSH
from testcase.utils.Constant import Constant


class SystemTable(unittest.TestCase):
    def setUp(self):
        self.log = Logger()
        self.log.info(f'-----{os.path.basename(__file__)} start-----')
        self.comsh = CommonSH('PrimaryDbUser')
        self.rootnode= Node('PrimaryRoot')
        self.usernode = Node('PrimaryDbUser')
        self.constant = Constant()
        self.com = Common()
        self.t_name = 't_mot_0299'
        self.java_name = 'jdbc_mot_case0299'
        self.param = 'enable_incremental_checkpoint'
        self.target_path = os.path.join(macro.DB_BACKUP_PATH, 'JDBC_test')

    def test_system_table(self):
        text = '-----step1:关闭enable_incremental_checkpoint参数; expect:成功-----'
        self.log.info(text)

        status = self.comsh.execute_gsguc('set', self.constant.GSGUC_SUCCESS_MSG,
                                          f'{self.param}=off')
        self.assertTrue(status, '执行失败' + text)

        restart = self.comsh.restart_db_cluster()
        self.log.info(restart)
        status_msg = self.comsh.get_db_cluster_status()
        self.assertTrue("Normal" or "Degrade" in status_msg)

        show_msg = self.com.show_param(f'{self.param}')
        self.assertIn("off", show_msg, '参数查看失败异常')

        text = '-----step2:编写java脚本，使用desc连接数据库,查询mot表数据; expect:远程复制java脚本至主机，成功;-----'
        self.log.info(text)

        self.com.scp_file(self.rootnode, f'{self.java_name}.java',self.target_path)
        check_cmd = f'ls {self.target_path}'
        check_res = self.rootnode.sh(check_cmd).result()
        self.log.info(check_res)
        self.assertIn(self.java_name, check_res, '执行失败' + text)

        text = '-----step3:编译java文件; expect:编译成功-----'
        self.log.info(text)

        self.rootnode.scp_put(macro.JDBC_PATH,
                              f'{self.target_path}/postgresql.jar')
        javac_cmd = f"javac -encoding utf-8 -cp " \
                    f"{os.path.join(self.target_path, 'postgresql.jar')} " \
                    f"{os.path.join(self.target_path, f'{self.java_name}.java')}"
        self.log.info(javac_cmd)

        javac_res = self.rootnode.sh(javac_cmd).result()
        self.log.info(javac_res)
        self.assertEqual('', javac_res, '执行失败' + text)

        text = '-----step4:运行java文件; expect:编译成功-----'
        self.log.info(text)

        url_host_port = f'{self.usernode.db_host}:{self.usernode.db_port}'
        java_cmd = f"java -cp " \
                   f"{os.path.join(self.target_path, 'postgresql.jar')}:{self.target_path} " \
                   f"{self.java_name} {url_host_port} {self.usernode.db_name} " \
                   f"{self.usernode.db_user} {self.usernode.db_password}"
        self.log.info(java_cmd)

        java_res = self.rootnode.sh(java_cmd).result()
        self.log.info(java_res)
        self.assertTrue('res_object1:3' in java_res and
                        'res_object1:2' in  java_res and
                        'res_object1:1' in  java_res, '执行失败' + text)

    def tearDown(self):
        self.log.info('-----This is teardown-----')
        text = '-----step5:恢复参数,清理编译脚本; expect：清理成功-----'
        self.log.info(text)
        reset = self.comsh.execute_gsguc('set', self.constant.GSGUC_SUCCESS_MSG,
                                         f'{self.param}=on')
        self.assertTrue(reset, '参数设置失败')
        restart = self.comsh.restart_db_cluster()
        self.log.info(restart)
        status = self.comsh.get_db_cluster_status()
        self.log.info(status)
        param = self.com.show_param(f'{self.param}')

        self.log.info('-----清理编译脚本-----')
        file_clean= f'rm -rf {self.target_path};' \
                    f'if [ -d {self.target_path} ];' \
                    f'then echo "exists"; else echo "not exists"; fi'
        self.log.info(file_clean)
        file_clean_res = self.rootnode.sh(file_clean).result()
        self.log.info(file_clean_res)

        self.assertIn("on", param, '恢复参数失败')
        self.assertEqual('not exists' ,file_clean_res, '清理脚本失败')
        self.log.info(f'-----{os.path.basename(__file__)} end-----')
